<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysUserDesigner;

/**
 * SysUserDesignerSearch represents the model behind the search form about `backend\models\SysUserDesigner`.
 */
class SysUserDesignerSearch extends SysUserDesigner
{
    /**
     * @inheritdoc
     */
    public $invite_f;
    public $user_type;
    public $username;
    public function rules()
    {
        return [
            [['uid', 'image_idcard_a', 'image_idcard_b', 'image_bcard', 'audit_state', 'audit_uid', 'audit_time'], 'integer'],
            [['name', 'company', 'address', 'id_card', 'audit_memo','invite_f','user_type','username'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params,$isDesigner=false,$inAddress=false)
    {
        $query = SysUserDesigner::find()->joinWith('invite')->joinWith('userinfo');
        $condition='';
        if ($isDesigner){
            $condition.="audit_state=1 ";
        }
            //
            $OperatorRelation=new OperatorRelation();
            $uid=Yii::$app->user->identity->user_id;
            $citylist_Str=$OperatorRelation-> getCityNameById($uid);
            if($citylist_Str!=""){
                if($condition!=""){
                    $condition.=" and address in ({$citylist_Str})";
                }else{
                    $condition.="   address in ({$citylist_Str})";
                }
               
            } 
        if (!empty($condition)){
            $query=$query->where($condition);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        
        $query->andFilterWhere([
            'sys_user_designer.uid' => $this->uid,
            'image_idcard_a' => $this->image_idcard_a,
            'image_idcard_b' => $this->image_idcard_b,
            'image_bcard' => $this->image_bcard,
            'audit_state' => $this->audit_state,
            'audit_uid' => $this->audit_uid,
            'audit_time' => $this->audit_time
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'id_card', $this->id_card])
            ->andFilterWhere(['=', 'user_invite.invite_f', $this->invite_f])
            ->andFilterWhere(['=', 'user_info.username', $this->username])
            ->andFilterWhere(['=', 'user_info.user_type', $this->user_type])
            ->andFilterWhere(['like', 'audit_memo', $this->audit_memo]);

        return $dataProvider;
    }
    
    public function search2($params,$isDesigner=false,$inAddress=false)
    {
        $query = SysUserDesigner::find()->joinWith('invite')->joinWith('userinfo');
        $condition="user_type in (3,4,7) ";
        if ($isDesigner){
            $condition.=" and audit_state=1";
        }
        $OperatorRelation=new OperatorRelation();
        $uid=Yii::$app->user->identity->user_id;
        $citylist_Str=$OperatorRelation-> getCityNameById($uid);
        $citylist_Str=strval($citylist_Str);
        
        if (!empty($citylist_Str)){
            $condition.="  and address in ({$citylist_Str})";
        }
        $query=$query->where($condition);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
    
        $this->load($params);
    
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
    
        $query->andFilterWhere([
            'sys_user_designer.uid' => $this->uid,
            'image_idcard_a' => $this->image_idcard_a,
            'image_idcard_b' => $this->image_idcard_b,
            'image_bcard' => $this->image_bcard,
            'audit_state' => $this->audit_state,
            'audit_uid' => $this->audit_uid,
            'audit_time' => $this->audit_time,
        ]);
    
        $query->andFilterWhere(['like', 'name', $this->name])
        ->andFilterWhere(['like', 'company', $this->company])
        ->andFilterWhere(['like', 'address', $this->address])
        ->andFilterWhere(['like', 'id_card', $this->id_card])
        ->andFilterWhere(['=', 'user_invite.invite_f', $this->invite_f])
        ->andFilterWhere(['=', 'user_info.username', $this->username])
        ->andFilterWhere(['=', 'user_info.user_type', $this->user_type])
        ->andFilterWhere(['like', 'audit_memo', $this->audit_memo]);
    
        return $dataProvider;
    }
}
